package com.component;


import com.alibaba.fastjson.JSON;
import com.dao.KafkaMapper;
import com.domain.KafkaMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class KafkaConsumer {

    @Autowired
    private KafkaMapper kafkaMapper;

    @KafkaListener(topics = {"autotest"})
    public void listen(ConsumerRecord<?, ?> record) {

        Optional.ofNullable(record.value())
                .ifPresent(message -> {
                    log.info("【+++++++++++++++++ record = {} 】", record);
                    log.info("【消费者接收message : {}】", message);
                    kafkaMapper.insert(JSON.parseObject(message.toString(), KafkaMsg.class));
                });

//      Optional msg = Optional.ofNullable(record.value());
//      if (msg.isPresent())
//      {
//          log.info("消费者消费消息：{}", msg.get());
//      }

    }


}
